import { postrent } from '../../../utils/api/module/task';
import {upLoadFile} from '../../../utils/api/module/upload';
import config from "../../../config";

Page({
    data: {
        URL: config._URL_,
        imgs:{
            upImg01:'/assets/img/upImg01@2x.png',
            upImg02:'/assets/img/upImg02@2x.png',
            uploaded: 'https://ythapi.bjyangshu.com/public/images/shengheed.png'
        },
        //名称
        service_name:'',
        // 上传图片
        uploadImgs: [],
        //控制弹窗
        toastFlag: true,
        //招租类型
        leaseTypeList: [{id: 0, name: '类型1'}, {id: 1, name: '类型2'}, {id: 2, name: '类型3'}, {id: 3, name: '类型4'}, {id: 4, name: '类型5'}],
        leaseType:{id: null, name: '请选择招租类型'} ,  //服务类型
        //可租面积
        leaseAreaList: [{id: 0, name:'100-200 m2'}, {id: 1, name:'200-300 m2'}, {id: 2, name:'300-400 m2'}, {id: 3, name:'400-500 m2'}, {id: 4, name:'500-600 m2'}],
        leaseArea:{id: null, name: '请选择可租面积'} ,
        // //租赁价格
        leasePriceList: [{id: 0, name:'1000-2000 元'}, {id: 1, name:'2000-3000 元'}, {id: 2, name:'3000-4000 元'}, {id: 3, name:'4000-5000 元'}, {id: 4, name:'5000-6000 元'}],
        leasePrice:{id: null, name: '请选择租赁价格'} ,
        // //装修程度
        zxPercentList: [{id: 0, name:'精装1'}, {id: 1, name:'精装2'}, {id: 2, name:'精装3'}, {id: 3, name:'精装4'}, {id: 4, name:'精装5'}],
        zxPercent:{id: null, name: '请选择装修程度'} ,
    },
    onLoad: function (options) {

    },

    onShow() {

    },
    /**
     * 服务名称双向绑定
     * @param e
     */
    serviceNameVal(e){
        this.setData({
            service_name: e.detail.value,
        })
    },
    /**
     * 点击显示选择类型
     */
    selecteType(e){
        console.log(e);
        this.setData({
            toastFlag: false,
            selectObj: {
                list: e.currentTarget.dataset.list,
                title: e.currentTarget.dataset.title,
            }
        })
    },
    /**
     * 关闭选择类型弹窗
     */
    closeToast(e){
        console.log(e.detail);
        switch (e.detail.ty) {
            case "选择招租类型":
                this.setData({ leaseType: e.detail });
                break;
            case "选择可租面积":
                this.setData({ leaseArea: e.detail });
                break;
            case "选择租赁价格":
                this.setData({ leasePrice: e.detail });
                break;
            case "选择装修程度":
                this.setData({ zxPercent: e.detail });
                break;
        }
        this.setData({
            toastFlag: true,
        })
    },
    /**
     * 点击上传图片
     */
    clickChooseImage(){
        let that = this;
        wx.chooseImage({
            count: 1 , // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                console.log(res);
                let {tempFilePaths} = res;
                upLoadFile({
                    formData:{
                        "mid": wx.getStorageSync('userInfo').mid
                    },
                    filePath: tempFilePaths[0],
                },{
                    "Cookie": 'SESSION_='+wx.getStorageSync('session_id'),
                }).then(res => {
                    console.log(res);
                    let data = JSON.parse(res.data);
                    console.log(data);
                    let imgs = [data.data.url];
                    console.log(imgs);
                    if(data.code == 1){
                        that.setData({
                            uploadImgs: imgs
                        })
                    }
                })
            }
        })
    },
    /**
     * 双向绑定地址
     * @param e
     */
    modelAddressVal(e){
        this.setData({
            addressVal:e.detail.value,
        })
    },
    /**
     * 双向绑定地铁
     * @param e
     */
    modelSubwayVal(e){
        this.setData({
            subwayVal: e.detail.value,
        })
    },
    /**
     * 双向绑定公交
     * @param e
     */
    modelBusVal(e){
        this.setData({
            busVal: e.detail.value,
        })
    },
    /**
     * 双向绑定联系人姓名
     * @param e
     */
    modelCallUserVal(e){
        this.setData({
            callUserVal: e.detail.value,
        })
    },
    /**
     * 双向绑定联系电话
     * @param e
     */
    modelCallPhoneVal(e){
        this.setData({
            callPhoneVal: e.detail.value,
        })

    },
    serviceGetVal(e){
        this.setData({
            serviceInfo: e.detail.value,
        })
    },
    /**
     * 发布任务
     */
    publisinfo(){
        let {service_name, uploadImgs, leaseType, leaseArea,leasePrice, zxPercent,addressVal, subwayVal, busVal, callUserVal, callPhoneVal, serviceInfo } = this.data;
        console.log(service_name, '------------->名称');
        console.log(uploadImgs[0], '------------->图片地址');
        console.log(leaseType, '------------->招租类型');
        console.log(leaseArea, '------------->可租面积');
        console.log(leasePrice, '------------->租赁价格');
        console.log(zxPercent, '------------->装修程度');
        console.log(addressVal, '-------------> 地址信息');
        console.log(subwayVal, '-------------> 附近地铁信息');
        console.log(busVal, '-------------> 附近公交信息');
        console.log(callUserVal, '-------------> 联系人信息');
        console.log(callPhoneVal, '-------------> 联系人电话');
        console.log(serviceInfo, '-------------> 详情介绍');
        postrent({
            type: 0,  //状态码：0为新添加，1为修改
            mid: wx.getStorageSync('userInfo').mid, // 用户ID

        }, {
            Cookie: 'SESSION_='+wx.getStorageSync('session_id'),
        }).then( res => {
            if(res.data.code == 1){
                wx.showToast({title: res.data.message, icon:"none"})
            }
        }).catch(error => {
            console.log(error);
        })
    }
});